home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 February / WN_129_CD.iso / Windows / Extensions Firefox / SwitchProxy Tool / switchproxy_1.3.1.xpi / chrome / switchproxy.jar / content / proxy.js < prev    next >
Encoding:
JavaScript  |  2004-08-05  |  36.8 KB  |  1,160 lines

  1. /*
  2. * 2004
  3. * Copyright Jeremy Gillick, All rights reserved
  4. */
  5.  
  6. /*
  7. *
  8. *     ERROR CODES
  9. *    100 - 149: switchproxy_setProxy()
  10. *    150 - 199: switchproxy_removeProxy()
  11. *    200 - 249: switchproxy_clearCookies()
  12. *    250 - 299: switchproxy_editProxyDialog()
  13. *    300    - 349: switchproxy_manualUpgradCheck()
  14. *
  15. */
  16.  
  17. var gSwitchP_List;
  18. var gSwitchP_ListPopup;
  19. var gSwitchP_ContextList;
  20. var gSwitchP_StatusBar;
  21. var gSwitchP_MenuList;
  22. var gSwitchP_ManageList;
  23. var gSwitchP_ElementList;
  24. var gSwitchP_ElementButton;
  25. var gSwitchP_LastItem;
  26. var gSwitchP_ProxyCount        = 0;
  27. var gSwitchP_InManager        = false;
  28. var gSwitchP_Loaded            = false;
  29. var gSwitchP_Cycle            = null;
  30. var gSwitchP_CommandEnd        = true;
  31. var gSwitchP_Prefs            = null;
  32. var gSwitchP_NoneLabel        = "None";
  33.  
  34. var gSwitchPTabMgr    = null;
  35.  
  36. function switchproxy_initProxy(event){
  37.     
  38.     window.removeEventListener("load", switchproxy_initProxy, true);
  39.  
  40.     if(!gSwitchP_Loaded){
  41.         /*if(typeof(switchproxy_TabManager) != 'undefined')
  42.             gSwitchPTabMgr    = new switchproxy_TabManager(); */
  43.  
  44.         gSwitchP_List            = document.getElementById('proxy-list')
  45.         gSwitchP_ListPopup        = document.getElementById('proxy-list-popup');
  46.         gSwitchP_ManageList        = document.getElementById('manage-proxy-list');
  47.         gSwitchP_ContextList    = document.getElementById('context-proxy-list');
  48.         gSwitchP_StatusBar        = document.getElementById('switchproxy-status');
  49.         gSwitchP_MenuList        = document.getElementById('switchproxy-menu-list');
  50.         gSwitchP_ElementList    = document.getElementById('switchproxy-element-list');
  51.         gSwitchP_ElementButton    = document.getElementById('switchproxy-element-button');
  52.         gSwitchP_Prefs            = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  53.         
  54.         gSwitchP_NoneLabel        = switchproxy_getString("common.proxy.none");
  55.         
  56.         //Is in Proxy Manager
  57.         if(gSwitchP_ManageList != null){
  58.             gSwitchP_InManager = true;
  59.             gSwitchP_List = gSwitchP_ManageList;
  60.         }
  61.         
  62.         switchproxy_showMenus(true);
  63.                         
  64.         //Cleanup RDF File
  65.         switchproxy_cleanupRdf();
  66.         
  67.         //Populate Proxy List
  68.         switchproxy_populateList();
  69.         
  70.         //Set Last Proxy Selected
  71.         if(gSwitchP_List != null)
  72.             gSwitchP_LastItem = gSwitchP_List.selectedItem;
  73.         
  74.         //Add Preferences Listener
  75.         var oProxyObserver = {
  76.             observe : function(subject, topic, data){ switchproxy_populateList(); }
  77.         };
  78.         var oMenuObserver = {
  79.             observe : function(subject, topic, data){ switchproxy_showMenus(false); }
  80.         };
  81.         var oPrefBranch    = Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
  82.             //oPrefBranch.addObserver("network.proxy", oPrefObserver, false);
  83.             oPrefBranch.addObserver("switchproxy.proxy.rdf.lastupdate", oProxyObserver, false);
  84.             oPrefBranch.addObserver("switchproxy.display", oMenuObserver, false);
  85.         
  86.         //Check for updates (after 3 second delay)
  87.         setTimeout("switchproxy_autoUpdate()", 3000);        
  88.         
  89.         //Auto-Update Anonymous Proxy Lists (after 10 minutes)
  90.         gAnonUpdateTimout = setTimeout("switchproxy_anon_autoUpdateLists()", 600000);
  91.         
  92.         // Start Anon Rotation
  93.         setTimeout("switchproxy_anon_nextProxy()", 1000);
  94.         
  95.         gSwitchP_Loaded = true;
  96.     }
  97.     else {
  98.         /*if(gSwitchPTabMgr)
  99.             gSwitchPTabMgr.tabLoading();*/
  100.     }
  101. }
  102.  
  103. // Return Tab Manager
  104. function switchproxy_getTabManager(){
  105.     return gSwitchPTabMgr;
  106. }
  107.  
  108. //Used for debugging
  109. function switchproxy_debugEcho(str){
  110.     if(str)
  111.         alert(str)
  112.     else
  113.         alert("test");
  114. }
  115.  
  116. //Displays the context menu based on what 
  117. // the user selects in the options
  118. function switchproxy_showMenus(onStartup){
  119.     try{    
  120.         //Show/Hide menus
  121.         var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  122.                     
  123.         // Status Bar
  124.         var isHidden = (oPrefs.prefHasUserValue("switchproxy.display.statusbar") && !oPrefs.getBoolPref("switchproxy.display.statusbar"))
  125.         document.getElementById("switchproxy-status").setAttribute("collapsed", isHidden);
  126.         
  127.         // If not thunderbird
  128.         if(navigator.userAgent.search(/Thunderbird/gi) < 0){
  129.         
  130.             //Context
  131.             var isHidden = (oPrefs.prefHasUserValue("switchproxy.display.context") && !oPrefs.getBoolPref("switchproxy.display.context"))
  132.             document.getElementById("switchproxy-context-menu").setAttribute("collapsed", isHidden);
  133.             document.getElementById("switchproxy-context-separator").setAttribute("hidden", isHidden);
  134.         
  135.             //Toolbar
  136.             if(onStartup){
  137.                 oPrefs.setBoolPref("switchproxy.display.toolbar", (!document.getElementById("proxy-toolbar").getAttribute("collapsed")));
  138.             }
  139.             else{
  140.                 var isHidden = (oPrefs.prefHasUserValue("switchproxy.display.toolbar") && !oPrefs.getBoolPref("switchproxy.display.toolbar"))
  141.                 document.getElementById("proxy-toolbar").setAttribute("collapsed", isHidden);
  142.             }
  143.         }
  144.                 
  145.     }catch(err){}
  146. }
  147.  
  148. // Show Element List
  149. function switchproxy_showElementList(event){
  150.     var oContext    = document.getElementById('context-proxy-list');
  151.     var sUri        = oContext.selectedItem.value;
  152.     var sType        = oContext.selectedItem.getAttribute("proxyType");
  153.     
  154.     // Anonymous Proxy Menu Item
  155.     if(sType == "3"){
  156.         document.getElementById('switchproxy-menu-anon').setAttribute("collapsed", false);
  157.         document.getElementById('switchproxy-menu-anon-seperator').setAttribute("collapsed", false);
  158.         
  159.         document.getElementById('switchproxy-context-anon').setAttribute("collapsed", false);
  160.         document.getElementById('switchproxy-context-anon-seperator').setAttribute("collapsed", false);
  161.         
  162.         document.getElementById('switchproxy-tools-anon').setAttribute("collapsed", false);
  163.         document.getElementById('switchproxy-tools-anon-seperator').setAttribute("collapsed", false);
  164.     }
  165.     else{
  166.         document.getElementById('switchproxy-menu-anon').setAttribute("collapsed", true);
  167.         document.getElementById('switchproxy-menu-anon-seperator').setAttribute("collapsed", true);
  168.         
  169.         document.getElementById('switchproxy-context-anon').setAttribute("collapsed", true);
  170.         document.getElementById('switchproxy-context-anon-seperator').setAttribute("collapsed", true);
  171.         
  172.         document.getElementById('switchproxy-tools-anon').setAttribute("collapsed", true);
  173.         document.getElementById('switchproxy-tools-anon-seperator').setAttribute("collapsed", true);
  174.     }
  175. }
  176.  
  177. //Returns is the entered menu is currently showing
  178. //    values are 'context' or 'toolbar'
  179. function switchproxy_menuShowing(sMenu){
  180.     var oObj = null;
  181.     
  182.     if(sMenu == "context"){
  183.         oObj = document.getElementById("switchproxy-context-menu");
  184.     }
  185.     else if(sMenu == "toolbar"){
  186.         oObj = document.getElementById("proxy-toolbar");
  187.     }
  188.     
  189.     if(oObj == null)
  190.         return false;
  191.     
  192.     return !eval(oObj.getAttribute("collapsed"));
  193. }
  194.  
  195.  
  196. //Clears current Proxy List
  197. function switchproxy_clearList(){
  198.     
  199.     gSwitchP_ProxyCount = 0;
  200.     
  201.     //Toolbar List
  202.     if(!gSwitchP_InManager && gSwitchP_List != null && gSwitchP_List.removeAllItems && !gSwitchP_List.open){
  203.         gSwitchP_List.removeAllItems();
  204.     }
  205.     //Manage Proxy Box
  206.     if(gSwitchP_InManager && gSwitchP_List != null){
  207.         aNodes = gSwitchP_List.childNodes;
  208.         for(var i = aNodes.length-1; i >= 0; i--){
  209.             gSwitchP_List.removeChild(aNodes[i]);
  210.         }
  211.     }
  212.     //Context List
  213.     if(gSwitchP_ContextList != null){
  214.         aNodes = gSwitchP_ContextList.childNodes;
  215.         for(var i = aNodes.length-1; i >= 0; i--){
  216.             gSwitchP_ContextList.removeChild(aNodes[i]);
  217.         }
  218.     }
  219.     //Toolbar Element List
  220.     if(gSwitchP_ElementList != null){
  221.         aNodes = gSwitchP_ElementList.childNodes;
  222.         for(var i = aNodes.length-1; i >= 0; i--){
  223.             if(aNodes[i].getAttribute("class") == "proxy-menu-item")
  224.                 gSwitchP_ElementList.removeChild(aNodes[i]);
  225.         }
  226.     }
  227.     // Tools Menu List
  228.     if(gSwitchP_MenuList != null){
  229.         aNodes = gSwitchP_MenuList.childNodes;
  230.         for(var i = aNodes.length-1; i >= 0; i--){
  231.             if(aNodes[i].getAttribute("class") == "proxy-menu-item")
  232.                 gSwitchP_MenuList.removeChild(aNodes[i]);
  233.         }
  234.     }
  235. }
  236.  
  237. //Add Item to lists
  238. function switchproxy_appendToList(sLabel, sValue, isSelected, iType){
  239.     var oItem = null;
  240.     
  241.     gSwitchP_ProxyCount++;
  242.     
  243.     try{
  244.     
  245.         //Toolbar List
  246.         if(!gSwitchP_InManager && gSwitchP_List != null && !gSwitchP_List.open){
  247.             oItem = gSwitchP_List.appendItem(sLabel, sValue);
  248.             oItem.setAttribute("oncommand", "switchproxy_anon_stopRotation(); switchproxy_selectProxy();");    
  249.             oItem.setAttribute("proxyType", iType);    
  250.         }
  251.         //Manage Proxy Box
  252.         if(gSwitchP_InManager && gSwitchP_List != null && sLabel != gSwitchP_NoneLabel){
  253.             oItem = gSwitchP_List.appendItem(sLabel, sValue);
  254.             oItem.setAttribute("proxyType", iType);    
  255.         }
  256.         //Context List
  257.         if(gSwitchP_ContextList != null){
  258.             oItem = document.createElement("menuitem");
  259.             oItem.setAttribute("label", sLabel);
  260.             oItem.setAttribute("value", sValue);
  261.             oItem.setAttribute("type", "checkbox");
  262.             oItem.setAttribute("proxyType", iType);    
  263.             oItem.setAttribute("oncommand", "switchproxy_queueSetProxy(this);");
  264.             gSwitchP_ContextList.appendChild(oItem);
  265.         }
  266.         //Toolbar Element List
  267.         if(gSwitchP_ElementList != null){
  268.             //Get separator, which is after proxy list
  269.             var oSeparator = document.getElementById("switchproxy-element-list-separator");
  270.             oItem = document.createElement("menuitem");
  271.             oItem.setAttribute("id", sValue);
  272.             oItem.setAttribute("label", sLabel);
  273.             oItem.setAttribute("value", sValue);
  274.             oItem.setAttribute("type", "checkbox");
  275.             oItem.setAttribute("proxyType", iType);    
  276.             oItem.setAttribute("class", "proxy-menu-item");
  277.             oItem.setAttribute("oncommand", "switchproxy_queueSetProxy(this);");
  278.             gSwitchP_ElementList.insertBefore(oItem, oSeparator);
  279.         }
  280.         // Tools Menu List
  281.         if(gSwitchP_MenuList != null){
  282.             //Get separator, which is after proxy list
  283.             var oSeparator = document.getElementById("switchproxy-menu-list-separator");
  284.             oItem = document.createElement("menuitem");
  285.             oItem.setAttribute("id", sValue);
  286.             oItem.setAttribute("label", sLabel);
  287.             oItem.setAttribute("value", sValue);
  288.             oItem.setAttribute("type", "checkbox");
  289.             oItem.setAttribute("proxyType", iType);    
  290.             oItem.setAttribute("class", "proxy-menu-item");
  291.             oItem.setAttribute("oncommand", "switchproxy_queueSetProxy(this);");
  292.             gSwitchP_MenuList.insertBefore(oItem, oSeparator);
  293.         }
  294.     
  295.         //Select
  296.         if(isSelected)
  297.             switchproxy_selectItem(sLabel);
  298.         
  299.     }catch(err){}
  300. }
  301.  
  302. //Sets List item with labe sLabel, to be selected/checked
  303. function switchproxy_selectItem(sLabel){
  304.  
  305.     try{
  306.         //Toolbar List
  307.         if(!gSwitchP_InManager && gSwitchP_List != null){
  308.             oItem = gSwitchP_List.getElementsByAttribute("label", sLabel);
  309.             if(oItem != null){
  310.                 oItem                 = oItem[0];
  311.                 gSwitchP_List.selectedItem    = oItem;
  312.                 gSwitchP_List.label            = oItem.getAttribute("label");
  313.                 oItem.setAttribute("selected", true);
  314.             }
  315.         }
  316.         //Context
  317.         if(gSwitchP_ContextList != null){
  318.             oItem = gSwitchP_ContextList.getElementsByAttribute("label", sLabel);
  319.             if(oItem != null){
  320.                 //Uncheck Other
  321.                 var aChecked = gSwitchP_ContextList.getElementsByAttribute("checked", "true");
  322.                 for(var c = 0; c < aChecked.length; c++){
  323.                     aChecked[c].setAttribute("checked", false);
  324.                 }
  325.             
  326.                 //Select
  327.                 oItem                        = oItem[0];
  328.                 gSwitchP_ContextList.selectedItem     = oItem;
  329.                 oItem.setAttribute("checked", true);
  330.             }
  331.         }
  332.         //Toolbar Element List
  333.         if(gSwitchP_ElementList != null){
  334.             oItem = gSwitchP_ElementList.getElementsByAttribute("label", sLabel);
  335.             if(oItem != null && oItem.length > 0){
  336.                 //Uncheck Other
  337.                 var aChecked = gSwitchP_ElementList.getElementsByAttribute("checked", "true");
  338.                 for(var c = 0; c < aChecked.length; c++){
  339.                     aChecked[c].setAttribute("checked", false);
  340.                 }
  341.                 
  342.                 //Check Selected
  343.                 oItem                        = oItem[0];
  344.                 gSwitchP_ElementList.selectedItem     = oItem;
  345.                 oItem.setAttribute("selected", true);
  346.                 oItem.setAttribute("checked", true);
  347.                 
  348.                 //Change Button Label
  349.                 gSwitchP_ElementButton    = document.getElementById('switchproxy-element-button');    
  350.                 if(gSwitchP_ElementButton != null){
  351.                     gSwitchP_ElementButton.setAttribute("label", sLabel);
  352.                 }
  353.             }
  354.         }
  355.         // Tools Menu List
  356.         if(gSwitchP_MenuList != null){
  357.             oItem = gSwitchP_MenuList.getElementsByAttribute("label", sLabel);
  358.             if(oItem != null && oItem.length > 0){
  359.             
  360.                 //Uncheck Other
  361.                 var aChecked = gSwitchP_MenuList.getElementsByAttribute("checked", "true");
  362.                 for(var c = 0; c < aChecked.length; c++){
  363.                     aChecked[c].setAttribute("checked", false);
  364.                 }
  365.                 
  366.                 //Check Selected
  367.                 oItem = oItem[0];
  368.                 gSwitchP_MenuList.selectedItem = oItem;
  369.                 oItem.setAttribute("selected", true);
  370.                 oItem.setAttribute("checked", true);
  371.             }
  372.         }
  373.         // Status Bar
  374.         if(gSwitchP_StatusBar != null){
  375.             gSwitchP_StatusBar.setAttribute("label", "Proxy: "+ sLabel);
  376.         }
  377.         switchproxy_clearTimeout(gSwitchP_Cycle);
  378.         
  379.     }catch(err){}
  380. }
  381.  
  382. //Return menuitem for the given sUri
  383. function switchproxy_getMenuItem(sUri){
  384.     var oItem = null;
  385.     
  386.     //Toolbar
  387.     oItem = (gSwitchP_List != null) ? gSwitchP_List.getElementsByAttribute("value", sUri) : null;
  388.     if(oItem != null && oItem[0] != null)
  389.         return oItem[0];
  390.     
  391.     //Context
  392.     oItem = (gSwitchP_List != null) ? gSwitchP_ContextList.getElementsByAttribute("value", sUri) : null;
  393.     if(oItem != null && oItem[0] != null)
  394.         return oItem[0];
  395.     
  396.     //Toolbar Element
  397.     oItem = (gSwitchP_List != null) ? gSwitchP_ElementList.getElementsByAttribute("value", sUri) : null;
  398.     if(oItem != null && oItem[0] != null)
  399.         return oItem[0];
  400.     
  401.     
  402.     //Default
  403.     return null;
  404. }
  405.  
  406. function switchproxy_cycle(sAction){
  407.     if(gSwitchP_Cycle != null){
  408.     
  409.         switchproxy_clearTimeout(gSwitchP_Cycle);
  410.         gSwitchP_CommandEnd = true;
  411.  
  412.         if(sAction == 'change' && gSwitchP_LastItem != null && gSwitchP_List != null && gSwitchP_List.selectedItem.label != gSwitchP_LastItem.label && gSwitchP_List.selectedIndex > 0){
  413.             gSwitchP_List.label                    = gSwitchP_LastItem.label;
  414.             gSwitchP_List.selectedItem            = gSwitchP_LastItem;
  415.             gSwitchP_List.selectedItem.value     = gSwitchP_LastItem.value;
  416.             
  417.             switchproxy_setProxy();
  418.         }
  419.         else if(sAction == 'select' && gSwitchP_List != null && gSwitchP_List.selectedIndex > -1){
  420.             switchproxy_setStatus(switchproxy_getString("toolbar.notApplied"), "#F00");
  421.             setTimeout("switchproxy_setStatus(switchproxy_getString('toolbar.loading'))", 3000);
  422.             setTimeout("switchproxy_populateList('"+ gSwitchP_LastItem.label +"');", 4000);
  423.             setTimeout("switchproxy_setProxy();", 4500);
  424.         }
  425.         
  426.     }
  427. }
  428.  
  429. //When user selects a Proxy from the menu list
  430. function switchproxy_selectProxy(){
  431.  
  432.     switchproxy_clearTimeout(gSwitchP_Cycle);
  433.     if(gSwitchP_List == null || gSwitchP_List.selectedItem == null || gSwitchP_InManager)
  434.         return;
  435.         
  436.     if(gSwitchP_List.selectedItem.label != gSwitchP_NoneLabel){
  437.         document.getElementById('edit-button').disabled = false;
  438.         document.getElementById('remove-button').disabled = false;
  439.         document.getElementById('edit-button').image = "chrome://switchproxy/content/icons/pencil.png";
  440.         document.getElementById('remove-button').image = "chrome://switchproxy/content/icons/cross.png";
  441.     
  442.     }
  443.     else{
  444.         document.getElementById('edit-button').disabled = true;
  445.         document.getElementById('remove-button').disabled = true;
  446.         document.getElementById('edit-button').image = "chrome://switchproxy/content/icons/pencil_disabled.png";
  447.         document.getElementById('remove-button').image = "chrome://switchproxy/content/icons/cross_disabled.png";
  448.     }
  449.     
  450.     if(typeof(gSwitchP_List.selectedItem.value) != 'undefined'){
  451.         //If this item is not being used by the browser, 
  452.         //    show a helpful message and cycle
  453.         var oPrefs    = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  454.         if((!oPrefs.prefHasUserValue("switchproxy.proxy.current") || oPrefs.getCharPref("switchproxy.proxy.current") != gSwitchP_List.selectedItem.value) && gSwitchP_LastItem != null && gSwitchP_LastItem != gSwitchP_List.selectedItem){
  455.             switchproxy_setStatus(switchproxy_getString("toolbar.clickApply"), "#060");
  456.         
  457.             //Cycle if user doesn't click 'Apply'
  458.             if(gSwitchP_Cycle == null)
  459.                 gSwitchP_Cycle = setTimeout("switchproxy_cycle('select')", 10000);
  460.         }
  461.         //If it is being used by the browser
  462.         else if(oPrefs.prefHasUserValue("switchproxy.proxy.current") && oPrefs.getCharPref("switchproxy.proxy.current") == gSwitchP_List.selectedItem.value){
  463.             switchproxy_clearTimeout(gSwitchP_Cycle);
  464.             
  465.             //Anonomous
  466.             if(gSwitchP_List.selectedItem.getAttribute("proxyType") == "3"){
  467.                 if(switchproxy_anon_getCurrent() == "unknown")
  468.                     switchproxy_setProxy(false);
  469.                     
  470.                 switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label +" ["+ switchproxy_anon_getCurrent() +"] ("+ switchproxy_anon_getCurrentCount() +")");
  471.             }
  472.             //Standard
  473.             else{
  474.                 switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label);
  475.             }
  476.         }
  477.         else{
  478.             switchproxy_clearTimeout(gSwitchP_Cycle);
  479.             switchproxy_setStatus(switchproxy_getString("toolbar.using.none"));
  480.         }
  481.     }
  482.     
  483.     return true;
  484. }
  485.  
  486. //Queue Set Proxy
  487. function switchproxy_queueSetProxy(oMenuItem){
  488.     switchproxy_clearTimeout(gSwitchP_Cycle);
  489.     setTimeout("switchproxy_setProxy(true, true, null, '"+ oMenuItem.label +"', '"+ oMenuItem.value +"', '"+ oMenuItem.getAttribute("proxyType") +"')", 10);
  490. }
  491.  
  492. //Set Proxy
  493. function switchproxy_setProxy(bClean, fromContextMenu, oMenuItem, sLabel, sUri, sType){
  494.  
  495.     //Start Command
  496.     gSwitchP_CommandEnd = false;    
  497.     switchproxy_clearTimeout(gSwitchP_Cycle);
  498.     switchproxy_anon_stopRotation();
  499.     
  500.     //If in Proxy Manager, do nothing
  501.     if(gSwitchP_InManager){
  502.         gSwitchP_CommandEnd = true;
  503.         return;
  504.     }
  505.     
  506.     var hasError    = false;
  507.     
  508.     //Set fromContextMenu
  509.     if(fromContextMenu == null)
  510.         fromContextMenu = false;
  511.     
  512.     //Get Proxy URI & Label
  513.     var sProxyUri    = "";
  514.     var sProxyLabel    = "";
  515.     var sProxyType    = 0;
  516.     if(sUri != null && sLabel != null){
  517.         sProxyUri = sUri;
  518.         sProxyLabel = sLabel;
  519.         
  520.         if(sType) sProxyType = sType;
  521.     }
  522.     else if(oMenuItem != null){
  523.         sProxyUri    = oMenuItem.getAttribute("value");
  524.         sProxyLabel    = oMenuItem.getAttribute("label");
  525.         sProxyType    = oMenuItem.getAttribute("proxyType");
  526.     }
  527.     else{
  528.         if(fromContextMenu == true){
  529.             sProxyUri    = gSwitchP_ContextList.selectedItem.value;
  530.             sProxyLabel    = gSwitchP_ContextList.selectedItem.label;
  531.             sProxyType    = gSwitchP_ContextList.selectedItem.getAttribute("proxyType");
  532.         }
  533.         else if(gSwitchP_List != null){
  534.             sProxyUri    = gSwitchP_List.selectedItem.value;
  535.             sProxyLabel    = gSwitchP_List.selectedItem.label;
  536.             sProxyType    = gSwitchP_List.selectedItem.getAttribute("proxyType");
  537.         }
  538.     }
  539.     
  540.     //Branch if Anonomous
  541.     if(sProxyType == "3"){
  542.         switchproxy_setStatus(switchproxy_getString("toolbar.loading"));
  543.         switchproxy_anon_loadProxy(sProxyUri, true);
  544.     }
  545.     else if(sProxyUri != ""){
  546.         switchproxy_setStatus(switchproxy_getString("toolbar.loading"));
  547.         
  548.         try{
  549.             //Change Browser Preferences
  550.             var aProps    = switchProxy_ds_getPropertyValuesFor(sProxyUri);
  551.             
  552.             for(key in gSwichProxy_options){
  553.                 
  554.                 //Get pref type and update preference
  555.                 var sPrefVal = "";
  556.                 if(typeof(gSwichProxy_options_defaults[key]) == "number"){
  557.                     gSwitchP_Prefs.setIntPref(gSwichProxy_options[key], parseInt(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
  558.                 }
  559.                 else if(typeof(gSwichProxy_options_defaults[key]) == "boolean"){
  560.                     gSwitchP_Prefs.setBoolPref(gSwichProxy_options[key], eval(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
  561.                 }
  562.                 else{ //String
  563.                     gSwitchP_Prefs.setCharPref(gSwichProxy_options[key], aProps[gSProxyRdfNodeUriRoot +"#"+ key]);
  564.                 }
  565.             }
  566.             
  567.             //Get Proxy Label
  568.             sProxyLabel = aProps[gSProxyRdfNodeName];
  569.             
  570.             //Update Status
  571.             switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ sProxyLabel);
  572.             
  573.             //Update Preference
  574.             gSwitchP_Prefs.setCharPref("switchproxy.proxy.current", sProxyUri);    
  575.         }
  576.         catch(err){
  577.             alert("100: "+ switchproxy_getString("error.unknown")+ "\n("+ err +")");
  578.             hasError = true;
  579.             
  580.             //Cycle back to 'None'
  581.             if(gSwitchP_Cycle == null && !fromContextMenu)
  582.                 gSwitchP_Cycle = setTimeout("switchproxy_cycle('change')", 500);
  583.         }
  584.     }
  585.     else{
  586.         //Turn off proxy pref
  587.         try{
  588.             gSwitchP_Prefs.setIntPref("network.proxy.type", 0);
  589.             
  590.             //Update Status
  591.             switchproxy_setStatus(switchproxy_getString("toolbar.using.none"));
  592.             
  593.             
  594.             //Update Preference
  595.             gSwitchP_Prefs.setCharPref("switchproxy.proxy.current", "");
  596.             
  597.         }catch(e){
  598.             alert("101: "+ switchproxy_getString("error.unknown"));
  599.             hasError = true;
  600.         }
  601.     }
  602.     
  603.     // Finalize
  604.     if(hasError){
  605.         //Update Status
  606.         switchproxy_setStatus(switchproxy_getString("error.proxy.load") +" " + sProxyLabel, "#F00");
  607.     }
  608.     else{
  609.     
  610.         //switchproxy_tab_notify(sProxyUri);
  611.     
  612.         //Select this proxy in all lists
  613.         switchproxy_selectItem(sProxyLabel);
  614.         
  615.         //Set Last Proxy
  616.         gSwitchP_LastItem = switchproxy_getMenuItem(sProxyUri);
  617.         
  618.         //Options
  619.         try{
  620.             if(bClean){
  621.                 // Clear Cookies
  622.                 if(typeof(gBrowser) != 'undefined' && gSwitchP_Prefs.prefHasUserValue("switchproxy.clear.cookies") && gSwitchP_Prefs.getBoolPref("switchproxy.clear.cookies")){
  623.                     switchproxy_clearCookies();
  624.                 }
  625.                 
  626.                 // Reload Page
  627.                 if((typeof(gBrowser) != 'undefined' && (!gSwitchP_Prefs.prefHasUserValue("switchproxy.reload.tab") || gSwitchP_Prefs.getBoolPref("switchproxy.reload.tab")))){
  628.                     try{
  629.                         var oTab = gBrowser.mCurrentBrowser;
  630.                             oTab.webNavigation.reload(nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE);
  631.                     } catch(err) {    }
  632.                 }
  633.                 
  634.                 // Set Update Pref
  635.                 gSwitchP_Prefs.setIntPref("switchproxy.proxy.rdf.lastupdate", (new Date()).getTime());
  636.             }
  637.             
  638.         }catch(err){alert("102: "+ switchproxy_getString("error.unknown") + "\n("+err+")");}
  639.     }
  640.  
  641.     //End Command
  642.     gSwitchP_CommandEnd = true;
  643.     
  644. }
  645.  
  646. //Test if the RDF Element, oRef, is the current proxy being used
  647. function switchproxy_isSelected(oRef){
  648.     
  649.     try{
  650.         var oPrefs         = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  651.         var aProps        = switchProxy_ds_getPropertyValuesFor(oRef.Value);
  652.         var sCurrUri    = "";
  653.         var iType        = parseInt(aProps[gSProxyRdfNodeUriRoot + "#networkProxyType"]);
  654.         
  655.         if(oPrefs.prefHasUserValue("switchproxy.proxy.current") && (sCurrUri = oPrefs.getCharPref("switchproxy.proxy.current")) == oRef.Value){
  656.                     
  657.             //Verify this is really what's being used in the browser
  658.             if(iType == oPrefs.getIntPref("network.proxy.type")){
  659.                 if(iType == 1){
  660.                     for(key in gSwichProxy_options){
  661.                         
  662.                         //Skip These Keys
  663.                         if(key == "networkProxyType" || key == "networkProxyAutoconfigURL")
  664.                             continue;
  665.                         
  666.                         //Skip if RDF has Default Values
  667.                         if( (key == "networkProxySOCKSVersion" && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "5" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == ""))
  668.                             || (key == "networkProxyNone" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "localhost, 127.0.0.1")){
  669.                             continue;                                
  670.                         }
  671.                         
  672.                         //If Pref and RDF doesn't have a value for this
  673.                         if(!oPrefs.prefHasUserValue(gSwichProxy_options[key]) 
  674.                             && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "0")){
  675.                             continue;
  676.                         }
  677.                         
  678.                         // If pref doesn't have this and the RDF record does
  679.                         //    return false
  680.                         if(!oPrefs.prefHasUserValue(gSwichProxy_options[key]) 
  681.                             && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "0")){
  682.                             return false;
  683.                         }
  684.                         
  685.                         
  686.                         //Get pref and convert to string
  687.                         var sPrefVal = "";
  688.                         if(oPrefs.getPrefType(gSwichProxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_INT)
  689.                             sPrefVal = oPrefs.getIntPref(gSwichProxy_options[key]) + "";
  690.                         if(oPrefs.getPrefType(gSwichProxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_BOOL)
  691.                             sPrefVal = oPrefs.getBoolPref(gSwichProxy_options[key]) + "";
  692.                         if(oPrefs.getPrefType(gSwichProxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_STRING)
  693.                             sPrefVal = oPrefs.getCharPref(gSwichProxy_options[key]);
  694.                                                 
  695.                         //Return false if isn't equal
  696.                         if(sPrefVal != aProps[gSProxyRdfNodeUriRoot +"#"+ key]){
  697.                             return false;
  698.                         }
  699.                     }
  700.                     return true;
  701.                 }
  702.                 //If type is 2 and the PAC url is the same
  703.                 else if(iType == 2 && oPrefs.prefHasUserValue("switchproxy.proxy.current") 
  704.                             && oPrefs.getCharPref("network.proxy.autoconfig_url") == aProps[gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL"]){
  705.                         return true;
  706.                 }    
  707.             }
  708.             //Anonomous
  709.             else if(iType == 3 && oPrefs.prefHasUserValue("network.proxy.http") && oPrefs.prefHasUserValue("network.proxy.http_port")){
  710.                 var sProxy = oPrefs.getCharPref("network.proxy.http") +":"+ oPrefs.getIntPref("network.proxy.http_port");
  711.                 
  712.                 //is sProxy in proxy list
  713.                 oTestRes = switchProxy_ds_getElementForValue(gSProxyRdfNodeUriRoot +"#proxy", sProxy);
  714.                 
  715.                 if(oTestRes instanceof Components.interfaces.nsIRDFResource && oTestRes.Value == oRes.Value){
  716.                     switchproxy_anon_loadProxy(oRef.Value, true);
  717.                     return true;
  718.                 }
  719.                 
  720.                 return false;
  721.             }
  722.         }
  723.         
  724.     }catch(err){}
  725.     
  726.     
  727.     return false;
  728. }
  729.  
  730. function switchproxy_removeProxy(){    
  731.     switchproxy_clearTimeout(gSwitchP_Cycle);
  732.     var oItem    = gSwitchP_List.selectedItem;
  733.     
  734.     //Can't delete 'None'
  735.     if(gSwitchP_List.selectedItem != null && gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel){
  736.         alert("150: "+ switchproxy_getString("error.remove.forbidden"));
  737.         return;
  738.     }
  739.     
  740.     if(!confirm(switchproxy_getString("confirm.remove")))
  741.         return;
  742.     
  743.     try{
  744.         //RDF
  745.             //Remove
  746.             switchProxy_ds_removeElement(oItem.value);
  747.             
  748.             //Cleanup
  749.             switchproxy_cleanupRdf();
  750.         
  751.         //Update List
  752.             switchproxy_populateList();
  753.             setTimeout("switchproxy_setProxy();", 300);
  754.     }
  755.     catch(err){
  756.         alert("151: "+ switchproxy_getString("error.remove") + "\n("+ err +")");
  757.     }
  758. }
  759.  
  760. function switchproxy_clearCookies(){
  761.     try{
  762.         var oCookies    = Components.classes["@mozilla.org/cookiemanager;1"].createInstance(Components.interfaces.nsICookieManager);
  763.             oCookies.removeAll();
  764.     }catch(err){
  765.         alert("200: "+ switchproxy_getString("error.cookie"));
  766.     }
  767.     return true;
  768. }
  769.  
  770.  
  771. function switchproxy_populateList(sSelectedLabel){
  772.     switchproxy_clearTimeout(gSwitchP_Cycle);
  773.     
  774.     var sProxyUri    = "";
  775.     var iSelected    = (gSwitchP_List != null) ? gSwitchP_List.selectedIndex : -1;
  776.     var aProxies    = new Array();
  777.         
  778.     //Don't populate if a local command has not finished or if list is open
  779.     if(!gSwitchP_CommandEnd){
  780.         gSwitchP_CommandEnd = true; //reset
  781.         return;
  782.     }
  783.     else{ //Start Command
  784.         gSwitchP_CommandEnd = false;
  785.     }
  786.     
  787.     //Load Elements
  788.     try{
  789.         
  790.         //Remove List Items & Add 'None'
  791.         switchproxy_clearList();
  792.         switchproxy_appendToList(gSwitchP_NoneLabel, "", true, 0);
  793.         switchproxy_selectItem(gSwitchP_NoneLabel);
  794.         
  795.         //Get and Sort Elements
  796.         var aProxies    = switchProxy_ds_getAllElements();
  797.             aProxies.sort(switchproxy_sortProxies);
  798.         
  799.         //Add to list
  800.         for(i = 0; i < aProxies.length; i++){
  801.         
  802.             if(typeof(aProxies[i]) == 'undefined')
  803.                 continue; 
  804.             
  805.             try{
  806.             
  807.                 oRes        = switchProxy_ds_getResource(aProxies[i]);
  808.                 sProxyName    = switchProxy_ds_getValueFor(oRes, switchProxy_ds_getResource(gSProxyRdfNodeName));
  809.                 sProxyType    = switchProxy_ds_getValueFor(oRes, switchProxy_ds_getResource(gSProxyRdfNodeUriRoot+ "#networkProxyType"));
  810.                 
  811.                 if(sProxyName != null){
  812.                     oItem = switchproxy_appendToList(sProxyName, aProxies[i], false, sProxyType);
  813.                     
  814.                     //Select Item
  815.                     if((sSelectedLabel != null && sSelectedLabel == sProxyName) || (sSelectedLabel == null && switchproxy_isSelected(oRes))){
  816.                         switchproxy_selectItem(sProxyName);
  817.                     }
  818.                 }
  819.                 
  820.             } catch(err) {}
  821.         }
  822.         
  823.         //Finish
  824.         gSwitchP_CommandEnd = true;
  825.         setTimeout("switchproxy_selectProxy();", 300);
  826.         
  827.     }catch(err){}
  828. }
  829.  
  830. /*    
  831. * Sort Proxy List
  832. *    + oResA and oResB are RDF URIs
  833. */
  834. function switchproxy_sortProxies(oResA, oResB){
  835.     try{
  836.         //Get Proxy Names
  837.         sValA = switchProxy_ds_getValueFor(switchProxy_ds_getResource(oResA), switchProxy_ds_getResource(gSProxyRdfNodeName));
  838.         sValB = switchProxy_ds_getValueFor(switchProxy_ds_getResource(oResB), switchProxy_ds_getResource(gSProxyRdfNodeName));
  839.         
  840.         
  841.         if (sValA < sValB)
  842.             return -1;
  843.         if (sValA == sValB)
  844.             return 0;
  845.         if (sValA > sValB)
  846.             return 1;
  847.     }
  848.     catch(e){ return 0; }
  849. }
  850.  
  851. /* Cleanup unused SwitchProxy data in RDF
  852. *     + Remove resources that have not been applied or used
  853. */
  854. function switchproxy_cleanupRdf(){
  855.         
  856.     try{
  857.         swithproxy_upgrade_rdf();
  858.         
  859.         var aElements    = switchProxy_ds_getAllElements();
  860.         for(var e = 0; e < aElements.length; e++){
  861.             oRes     = switchProxy_ds_getResource(aElements[e]);
  862.             aProps    = switchProxy_ds_getPropertyValuesFor(aElements[e]);
  863.             
  864.             //If does not have a name property, 
  865.             //    then it is not listed -- so it's junk
  866.             if(aProps[gSProxyRdfNodeName] == null){
  867.                 switchProxy_ds_removeElement(aElements[e]);
  868.             }
  869.         }
  870.     }catch(err){}
  871. }
  872.  
  873. // Automatic Upgrade
  874. /*
  875. *    From Version 0.4
  876. *     + Add preference 'switchproxy.version'
  877. *    + Add preference 'switchproxy.proxy.current'
  878. *    + Change proxy's URI to random number
  879. *    + Convert #proxy to networkProxyAutoconfigURL
  880. *    + Add elements for full manual proxy configuration
  881. *        + view the globals.js gSwichProxy_options array for full list
  882. */
  883. function swithproxy_upgrade_rdf(){
  884.     
  885.     try{
  886.         
  887.         //Object
  888.         var oUriRegExp     = new RegExp("\\/sp_proxy_[0-9]{5,6}$");
  889.         var oPrefs         = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  890.         var aElements    = switchProxy_ds_getAllElements();
  891.         
  892.         
  893.         //If perference version is current version, don't upgrade
  894.         if(oPrefs.prefHasUserValue("switchproxy.version") && oPrefs.getCharPref("switchproxy.version") == gSwitchProxy_Version){
  895.             return;
  896.         }
  897.         
  898.         //Set Version
  899.         oPrefs.setCharPref("switchproxy.version", gSwitchProxy_Version);
  900.         
  901.         //Loop through elements
  902.         for(var e = 0; e < aElements.length; e++){
  903.             var oRes    = switchProxy_ds_getResource(aElements[e]);
  904.             var aAttrs     = switchProxy_ds_getPropertiesFor(aElements[e]);
  905.                         
  906.             //(v0.4) Change '#proxy' URI
  907.             if(aAttrs != null && typeof(aAttrs["proxy"]) != 'undefined' && aAttrs["proxy"] instanceof Components.interfaces.nsIRDFResource){
  908.                 switchProxy_ds_renamePropertyUri(oRes, aAttrs["proxy"], gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL");
  909.             }
  910.             
  911.             //(v0.4) Add new Properties
  912.             for(sAttrName in gSwichProxy_options){
  913.                 var oProp = switchProxy_ds_getResource(gSProxyRdfNodeUriRoot +"#"+ sAttrName);
  914.                 var sValue = gSwichProxy_options_defaults[sAttrName]; //Default value
  915.                 
  916.                 if(sValue != null)
  917.                     switchProxy_ds_addProperty(oRes, oProp, sValue, false);
  918.             }
  919.             
  920.             //(v0.4) Change URI and ID property to random number
  921.             if(!oUriRegExp.test(aElements[e])){
  922.                 var sUri = switchproxy_getUniqueProxyUri();
  923.                 switchProxy_ds_changePropertyValue(oRes, gSProxyRdfNodeId, sUri);
  924.                 switchProxy_ds_changeElementUri(oRes, sUri);
  925.             }
  926.             
  927.         }
  928.         
  929.         //Preferences            
  930.             //(v0.4) Add 'switchproxy.proxy.current'
  931.             if(!oPrefs.prefHasUserValue("switchproxy.proxy.current")){
  932.                 //Get & Set selected Proxy
  933.                 if(oPrefs.prefHasUserValue("network.proxy.autoconfig_url")){
  934.                     var sProxyUrl     = oPrefs.getCharPref("network.proxy.autoconfig_url");
  935.                     var oRes        = switchProxy_ds_getElementForValue(gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL", sProxyUrl);
  936.                                         
  937.                     if(oRes != null){
  938.                         oPrefs.setCharPref("switchproxy.proxy.current", oRes.Value);
  939.                     }
  940.                 }
  941.             }
  942.         
  943.     }catch(err){}
  944. }
  945.  
  946. //Generates random number for proxy URI
  947. function switchproxy_getUniqueProxyUri(){
  948.     var sUri = gSProxyRdfRoot + "/sp_proxy_" + Math.round((Math.random() * 200000));
  949.     
  950.     if(switchProxy_ds_doesProxyElementExist(sUri))
  951.         return switchproxy_getUniqueProxyUri();
  952.     
  953.     return sUri;
  954. }
  955.  
  956. function switchproxy_setStatus(sMsg, sColor){
  957.     if(!sColor)
  958.         sColor = "#000";
  959.     
  960.     oStatus = document.getElementById('status-text');
  961.     
  962.     if(oStatus != null){
  963.         document.getElementById('status-text').style.color = sColor;
  964.         document.getElementById('status-text').value = sMsg;
  965.     }
  966. }
  967.  
  968. //Clear Timeout
  969. function switchproxy_clearTimeout(){
  970.     clearTimeout(gSwitchP_Cycle);
  971.     gSwitchP_Cycle = null;
  972. }
  973.  
  974. //Execute a command when system is ready
  975. //    iCurrCount is for internal use, do not
  976. //    pass this argument
  977. function switchproxy_doCommand(sCommand, iLimitCount){
  978.     
  979.     if(iLimitCount == null)
  980.         iLimitCount = 0;
  981.     
  982.     //Wait for current command to end
  983.     if(!gSwitchP_CommandEnd && iLimitCount < 10){
  984.         iLimitCount++
  985.         setTimeout("switchproxy_doCommand('"+ sCommand +"', "+ iCurrCount +")", 100);
  986.     }
  987.     else{
  988.         gSwitchP_CommandEnd = true;
  989.         setTimeout(sCommand, 100);
  990.     }
  991. }
  992.  
  993. /*
  994. * Launch Dialogs
  995. */
  996.     
  997.     //Edit Proxy Dialog
  998.     function switchproxy_editProxyDialog(isNew){
  999.         switchproxy_clearTimeout(gSwitchP_Cycle);
  1000.         
  1001.         var sAction = null;
  1002.         
  1003.         //Nothing Selected
  1004.         var oEditItem = null
  1005.         if(!isNew && (oEditItem = gSwitchP_List.selectedItem) == null){
  1006.             alert("250: "+ switchproxy_getString("error.edit.select"));
  1007.             return;
  1008.         }
  1009.         
  1010.         //Can't edit 'None'
  1011.         if(!gSwitchP_InManager && !isNew && gSwitchP_List != null && gSwitchP_List.selectedItem != null && gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel){
  1012.             alert("251: "+ switchproxy_getString("error.edit.forbidden"));
  1013.             return;
  1014.         }
  1015.         
  1016.         //Edit
  1017.         if(!isNew){
  1018.             
  1019.             //Anon
  1020.             if(oEditItem.getAttribute("proxyType") == "3"){
  1021.                 window.openDialog("chrome://switchproxy/content/dialogs/editanon.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
  1022.             }
  1023.             //Standard
  1024.             else{
  1025.                 window.openDialog("chrome://switchproxy/content/dialogs/editproxy.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
  1026.             }
  1027.         }
  1028.         //Add
  1029.         else{
  1030.             window.openDialog("chrome://switchproxy/content/dialogs/addproxytype.xul","editproxy","centerscreen, chrome", "add");
  1031.         }
  1032.     }
  1033.     
  1034.     //Manage Proxy Dialog
  1035.     function switchproxy_openProxyManager(){
  1036.         window.openDialog("chrome://switchproxy/content/dialogs/manager.xul","manageproxies","centerscreen, chrome, resizable");
  1037.     }
  1038.  
  1039. /*
  1040. * Options
  1041. */
  1042.     function switchproxy_openSwitchProxyPrefs(){        
  1043.         window.openDialog("chrome://switchproxy/content/options/options.xul","switchProxyPrefs","centerscreen, chrome");
  1044.     }
  1045.     
  1046.     function switchproxy_goToSwitchProxySite(fromDialog){
  1047.         
  1048.         // If Thunderbird
  1049.         if(navigator.userAgent.search(/Thunderbird/gi) > -1){
  1050.             var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();         
  1051.                 messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger);
  1052.                 messenger.launchExternalURL(gSwitchProxy_DownloadSite +"?r=switchproxy");  
  1053.         }
  1054.         else{
  1055.             opener.gBrowser.selectedTab = opener.getBrowser().addTab(gSwitchProxy_DownloadSite +"?r=switchproxy");
  1056.         }
  1057.         self.close();
  1058.     }
  1059.     
  1060.     function switchproxy_goToSwitchProxyAbout(){
  1061.         window.openDialog("chrome://switchproxy/content/dialogs/about.xul","switchProxyAbout","centerscreen, chrome, modal");
  1062.     }
  1063.  
  1064. /*
  1065. * Automatic Updator
  1066. */
  1067.  
  1068.     function switchproxy_autoUpdate(){
  1069.         
  1070.         try{
  1071.         
  1072.             //Alert User
  1073.             var oPrefs    = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  1074.             if(!oPrefs.prefHasUserValue("switchproxy.update.notify") || oPrefs.getBoolPref("switchproxy.update.notify")){
  1075.                 
  1076.                 //Has 24 hours past since last notification
  1077.                 var iYesterday = ((new Date()).getTime() / 1000) - 86400;
  1078.                 if( (!oPrefs.prefHasUserValue("switchproxy.update.notify.time") || oPrefs.getIntPref("switchproxy.update.notify.time") <= iYesterday)
  1079.                     && (sXpi = switchproxy_needsUpgrade()) != false){
  1080.                 
  1081.                     //Update timestamp
  1082.                     oPrefs.setIntPref("switchproxy.update.notify.time", ((new Date()).getTime() / 1000));
  1083.                     
  1084.                     //Show Notification Dialog
  1085.                     window.openDialog("chrome://switchproxy/content/dialogs/update.xul","switchProxyUpdate","centerscreen, chrome, modal", sXpi.Value);
  1086.                 }
  1087.                 
  1088.             }
  1089.             
  1090.         }catch(err){}
  1091.     }
  1092.     
  1093.     //If there is a new version, returns the XPI url, else returns false
  1094.     function switchproxy_needsUpgrade(){
  1095.         
  1096.         const sThisVersion    = gSwitchProxy_Version;
  1097.         const sRdfUri         = "http://jgillick.nettripper.com/plugins.rdf?p=switchProxy";
  1098.         const sProductUri    = "http://jgillick.nettripper.com/rdf/switchproxy";
  1099.         
  1100.         try{
  1101.             // Get current version and XPI from remote RDF
  1102.             var oRdf            = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  1103.             var oDs                = oRdf.GetDataSourceBlocking(sRdfUri);
  1104.             var oProduct        = oRdf.GetResource(sProductUri);
  1105.                     
  1106.             var sVersion        = oDs.GetTarget(oProduct, oRdf.GetResource("http://jgillick.nettripper.com/rdf#version"), true);
  1107.             var sXpi            = oDs.GetTarget(oProduct, oRdf.GetResource("http://jgillick.nettripper.com/rdf#xpi"), true);
  1108.             
  1109.             //Not literals
  1110.             if( !(sVersion instanceof Components.interfaces.nsIRDFLiteral)
  1111.                 || !(sXpi instanceof Components.interfaces.nsIRDFLiteral)){
  1112.                 return false;
  1113.             }
  1114.             //No Update
  1115.             else if(sThisVersion >= sVersion.Value){
  1116.                 return false
  1117.             }
  1118.             else{
  1119.                 return sXpi;
  1120.             }
  1121.         }catch(err){ return false; }
  1122.     }
  1123.     
  1124.     //The user selects to check for updates
  1125.     function switchproxy_manualUpgradCheck(){
  1126.         
  1127.         var sXpi = switchproxy_needsUpgrade();
  1128.         
  1129.         if(sXpi != false){
  1130.             if(confirm(switchproxy_getString("upgrade.install"))){
  1131.                 try{
  1132.                     var aInstall = new Array();
  1133.                     aInstall["SwitchProxy"] = sXpi;
  1134.                     InstallTrigger.install(aInstall, null);
  1135.                     
  1136.                 }catch(err){ alert("300: "+ switchproxy_getString("error.update") +"\n"+err); }
  1137.             }
  1138.         }
  1139.         else{
  1140.             alert(switchproxy_getString("upgrade.none"));
  1141.         }
  1142.     
  1143.     }
  1144.     
  1145. /*
  1146. * UNINSTALL
  1147. */
  1148.     function switchproxy_uninstall(oLogger){
  1149.         try{
  1150.             var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  1151.                 oPrefs.deleteBranch("switchproxy");
  1152.         
  1153.             oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.SUCCESS_STATUS, "Preferences Branch 'switchproxy'", null, null));
  1154.         }catch(err){
  1155.             oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.WARN_STATUS, "Preferences Branch 'switchproxy'", 1001, err));
  1156.             throw 1001;
  1157.         }
  1158.     }
  1159.  
  1160.